"use strict";

(function() {
	// getBoolPref(), setBoolPref(), getCharPref(), setCharPref(), getIntPref() and setIntPref().
	var prefs = {};
	
	var root = (function() {	
		var prefService = Cc["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService);
		var branch = prefService.getBranch("extensions.linkjuggernaut.");
				
		return branch;
	})();
	
	var typeHandlers = {
		"i": { get: root.getIntPref,  set: root.setIntPref },
		"c": { get: root.getCharPref, set: root.setCharPref },
		"b": { get: root.getBoolPref, set: root.setBoolPref }
	};
			
	var handlePref = function(name, type, val) {
		if (val) {
			typeHandlers[type].set(name, val);
			return val;
		}
		else {
			return typeHandlers[type].get(name);
		}
	};
	
	prefs.visual = {
		rectRoundedX:  function(val) {
			return handlePref("visual.rectRoundedX", "i", val);
		},
		rectRoundedY:  function(val) {
			return handlePref("visual.rectRoundedY", "i", val);
		},
			
		strokeWidth:  function(val) {
			return handlePref("visual.strokeWidth", "i", val);
		},
		strokeColor:  function(val) {
			return handlePref("visual.strokeColor", "c", val);
		},
		fillColor:  function(val) {
			return handlePref("visual.fillColor", "c", val);
		},
		fillOpacity:  function(val) {
			return handlePref("visual.fillOpacity", "c", val);
		},
		strokeLineCap: function(val) {
			return handlePref("visual.strokeLineCap", "c", val);
		},
		strokeLineJoin: function(val) {
			return handlePref("visual.strokeLineJoin", "c", val);
		},
		strokeDashArray: function(val) {
			return handlePref("visual.strokeDashArray", "c", val);
		},
		
		itemSelRectColor:  function(val) {
			return handlePref("visual.itemSelRectColor", "c", val);
		},
		itemSelRectOpacity:  function(val) {
			return handlePref("visual.itemSelRectOpacity", "c", val);
		},
		itemSelRectBorderWidth:  function(val) {
			return handlePref("visual.itemSelRectBorderWidth", "i", val);
		},
		itemSelRectPadding:  function(val) {
			return handlePref("visual.itemSelRectPadding", "i", val);
		}
	};
	
	prefs.trigger = {
		mouseButton:  function(val) {
			return handlePref("trigger.mouseButton", "i", val);
		},
		minPixelDrag:  function(val) {
			return handlePref("trigger.minPixelDrag", "i", val);
		},
		ctrlRequired: function(val) {
			return handlePref("trigger.ctrlRequired", "b", val);
		},
		altRequired: function(val) {
			return handlePref("trigger.altRequired", "b", val);
		},
		shiftRequired: function(val) {
			return handlePref("trigger.shiftRequired", "b", val);
		}
	};
	
	prefs.keys = {
		cancelSelection: function (val) {
			return handlePref("keys.cancelSelection", "i", val);
		}	
	};
	
	prefs.selectionModes = {
			rect: {
				hitPercentageInclusion: function (val) {
					return handlePref("selectionModes.rect.hitPercentageInclusion", "c", val);
				}
			}
	};
	
	prefs.general = {
		selectionMode: function(val) {
			return handlePref("general.selectionMode", "c", val);
		}
	};
	
	alx.lj.prefs = prefs;
})();